<%@ page import="java.io.File,java.io.FilenameFilter,java.util.Arrays" %>
<%! static String tree;
    static Object lock = new Object();

    private static void build(javax.servlet.jsp.JspWriter myout) throws Exception {
        StringBuilder sb = new StringBuilder();
        String dir = "/home/zwu/Documents/gdoc"; //request.getParameter("dir");
        if (dir == null) {
            return;
        }

        if (dir.charAt(dir.length() - 1) == '\\') {
            dir = dir.substring(0, dir.length() - 1) + "/";
        } else if (dir.charAt(dir.length() - 1) != '/') {
            dir += "/";
        }

        dir = java.net.URLDecoder.decode(dir, "UTF-8");

        if (new File(dir).exists()) {
            String[] files = new File(dir).list(new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.charAt(0) != '.';
                }
            });
            Arrays.sort(files, String.CASE_INSENSITIVE_ORDER);
            sb.append("<ul class=\"jqueryFileTree\" style=\"display: none;\">");
            // All dirs
            for (String file : files) {
                if (new File(dir, file).isDirectory()) {
                    sb.append("<li class=\"directory collapsed\"><a href=\"#\" rel=\"" + dir + file + "/\">"
                            + file + "</a></li>");
                }
            }
            // All files
            for (String file : files) {
                if (!new File(dir, file).isDirectory()) {
                    int dotIndex = file.lastIndexOf('.');
                    String ext = dotIndex > 0 ? file.substring(dotIndex + 1) : "";
                    sb.append("<li class=\"file ext_" + ext + "\"><a href=\"#\" rel=\"" + dir + file + "\">"
                            + file + "</a></li>");
                }
            }
            sb.append("</ul>");
        }
        synchronized (lock) {
            tree = sb.toString();
        }
    }

%>
<%
 
    if (tree == null) {
        build(out);
    }

    out.println(tree);
%>